package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.CardType;
import com.aduerr.v1shouyin.entity.KaQuanHeXiaoEntity;
import com.aduerr.v1shouyin.entity.KaQuanXiangQingEntity;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.util.TextUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.internal.observers.BlockingBaseObserver;
import io.reactivex.schedulers.Schedulers;

import java.util.HashMap;

/**
 * description：
 * author：Marco
 * creatTime：2018/9/27
 * version：1
 */
public class KaQuanInputMoneyActivity extends AppCompatActivity {

    private static int defalutBackIconResID;

    static {
        defalutBackIconResID = R.drawable.img_back;
    }

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;
    @BindView(R.id.card_num)
    TextView tvCardNum;
    @BindView(R.id.edt_money)
    EditText edtMoney;
    @BindView(R.id.btn_hexiao)
    Button btnHexiao;

//    Handler handler=new Handler(){
//        @Override
//        public void dispatchMessage(Message msg) {
//            super.dispatchMessage(msg);
//            if (!Money.startsWith("￥") || Money.length()!=0) {
//                String ChangMoney = TextUtils.formatTosepaa(Money);
//                edtMoney.setText("￥" + ChangMoney);
//                btnHexiao.setEnabled(true);
//            }
//        }
//    };
    String erWeiMaResult;
    String Money;
    KaQuanXiangQingEntity.DataBean.ListBean kaquanxiangqingEntity;
    String changeName;

    public void initToolbarWithBack(int backResId) {
        tvToolbarTitle.setText("卡券详情");
        if(mToolbar != null) {
            int rid = backResId == 0 ? defalutBackIconResID : backResId;
            mToolbar.setNavigationIcon(rid);
            mToolbar.setNavigationOnClickListener(v -> finish());
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kaquaninput);
        ButterKnife.bind(this);
        initToolbarWithBack(0);
        initUI();
//        edtMoney.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

    }

    private void initUI() {
        erWeiMaResult = getIntent().getStringExtra("erWeiMaResult");
        kaquanxiangqingEntity =
                (KaQuanXiangQingEntity.DataBean.ListBean) getIntent().getSerializableExtra("Data");
        tvCardNum.setText(erWeiMaResult);
        edtMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!s.toString().contains("￥") && s.length() >= 1) {
                    changeName = TextUtils.formatToseparatow(s.toString());
                    edtMoney.setText(Html.fromHtml("<foot><small>￥</small></foot>" + changeName));
                    edtMoney.setSelection(edtMoney.getText().length());
                }
                if(s.length() != 0) {
                    Money = edtMoney.getText().toString();
                    btnHexiao.setEnabled(true);
                }
            }
        });
    }

    @OnClick({R.id.btn_hexiao})
    public void onViewClick(View view) {
        switch(view.getId()) {
            case R.id.btn_hexiao:
                responseNetForHexiao(erWeiMaResult, Money);
                break;

        }
    }

    //请求网络为了 核销该张优惠券
    private void responseNetForHexiao(String scanTicketNo, String minmoney) {
        HashMap<String, String> addParams = new HashMap<>();
        addParams.put("scanTicketNo", scanTicketNo);//86008032893055438199  test
        addParams.put("payMoney", minmoney.replace("￥", ""));
        APIRetrofit.getAPIService().getKaQuanHeXiao(RXRequest.getXingPosParams(addParams, this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BlockingBaseObserver<KaQuanHeXiaoEntity>() {
                    @Override
                    public void onNext(KaQuanHeXiaoEntity kaQuanHeXiaoEntity) {
                        int success = kaQuanHeXiaoEntity.getSuccess();
                        if(success == 1) {
                            Intent intent = new Intent(KaQuanInputMoneyActivity.this,
                                    CardUseSuccessNewActivity.class);
                            intent.putExtra("type", CardType.WX.name());
                            intent.putExtra("Data", kaquanxiangqingEntity);
                            intent.putExtra("kaquan", kaQuanHeXiaoEntity);
                            intent.putExtra("tag", "xianxia");
                            intent.putExtra("erWeiMaResult", erWeiMaResult);
                            startActivity(intent);
                            finish();
//                            String needpay = kaQuanHeXiaoEntity.getData().getNeedpay();
//                            double Dneedpay = Double.parseDouble(needpay);
//                            if (Dneedpay > 0) {
//                                //之前版本!!
//                                intent.putExtra("paymoney", needpay);
//                            }
//                            startActivity(intent);
//                            finish();
                        } else {
                            String errMsg = kaQuanHeXiaoEntity.getErrMsg();
                            ToastUtils.showToast(KaQuanInputMoneyActivity.this,
                                    errMsg + "");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
